package com.tianzeculture.stagebao.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.view.MyRecyclerview;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/27.
 */

public class SelectPopWindow extends PopupWindow implements PopWindowSelectAdapter.SelectLitenner {
    Activity context;
    List<String> list;
    private View styleView;
    private LayoutInflater inflater;
    private Display display;
    private WindowManager windowManager;

    MyRecyclerview mRecyclerview;
    PopWindowSelectAdapter adapter;

    public SelectPopWindow(Activity context) {
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        styleView = inflater.inflate(R.layout.popwindow_select, null);
        this.setContentView(styleView);
        //设置CityPickerPopWindow弹出窗体的宽
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.setWidth(RelativeLayout.LayoutParams.FILL_PARENT);
        //设置CityPickerPopWindow弹出窗体的高
//        this.setHeight((int) (display.getHeight() * 0.35));
        this.setHeight((int) (RelativeLayout.LayoutParams.WRAP_CONTENT));

        //设置CityPickerPopWindow弹出窗体可点击
        this.setFocusable(true);
        //设置CityPickerPopWindow可触摸
        this.setTouchable(true);
        //设置CityPickerPopWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x9000000);
        //设置CityPickerPopWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        initView();
    }

    private void initView() {
        list = new ArrayList<>();
        list.add("不限");
        list.add("男");
        list.add("女");
        mRecyclerview = styleView.findViewById(R.id.myRecyclerview);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setLayoutManager(manager);
        mRecyclerview.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        adapter = new PopWindowSelectAdapter(R.layout.item_popwindow_select, list);
        adapter.setLitenner(this);
        mRecyclerview.setAdapter(adapter);
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void onClick(String item) {
        litenner.isSelect(item);
        dismiss();
    }

    public void setLitenner(SelectWindowLitenner litenner) {
        this.litenner = litenner;
    }

    SelectWindowLitenner litenner;

    public interface SelectWindowLitenner {
        void isSelect(String str);

        void noticeSexCancel();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        litenner.noticeSexCancel();
    }
}
